<template>
  <div class="goods-wrap">
    <div class="goods-left">
      <van-sidebar v-model="activeKey">
        <van-sidebar-item
          v-for="(item, index) in goodsList"
          :key="index"
          :title="item.name"
          @click="sideBarClick"
        />
      </van-sidebar>
    </div>
    <div class="goods-rigth">
      <div class="single-box">
        <div
          class="goods-wrap-cont"
          v-for="(v, i) in goodsList"
          :key="i + 'rigth'"
          :id="i"
        >
          <div class="goods-cate-wrap">
            <span>{{ v.name }}</span>
          </div>
          <div
            class="goods-list"
            v-for="(food, i) in v.foods"
            :key="i + '1'"
            @click="handleFoodJump(food)"
          >
            <div class="list-left-img">
              <van-image fit="cover" :src="food.imgUrl" />
            </div>
            <div class="list-rigth-cont">
              <div>{{ food.name }}</div>
              <div>特惠</div>
              <div class="money-count">
                <span>￥{{ food.price }}</span>
                <p>
                  <button @click.stop="handleChangeCount(food.id, 'Minus')">
                    -
                  </button>
                  <span>{{ food.count }}</span>
                  <button @click.stop="handleChangeCount(food.id, 'Add')">
                    +
                  </button>
                </p>
              </div>
            </div>
          </div>
        </div>
      </div>
    </div>
  </div>
</template>

<script>
// import { api_goods_list } from "@/apis/goods";
import BScroll from "better-scroll";
import { mapState, mapMutations } from "vuex";
export default {
  name: "goodsList",
  data() {
    return {
      activeKey: 0,
      // goodsList: [],
      betterScroll: null,
    };
  },
  created() {
    // console.log("goods-created");
    // this.getGoodsList();
  },
  mounted() {
    // console.log("goods-mounted");
    this.betterScroll = new BScroll(".goods-rigth", {
      probeType: 3,
      click: true,
    });
    // 监听右侧是否滚动了
    this.betterScroll.on("scroll", ({ y }) => {
      let _y = Math.abs(y);
      this.getPoint.forEach((item) => {
        if (_y >= item.start && _y < item.end) {
          this.activeKey = item.id;
        }
      });
    });
  },
  computed: {
    ...mapState(["goodsList"]),
    getPoint() {
      // 获取盒子高度
      let arrH = [];
      this.goodsList.forEach((item, index) => {
        let h = document.getElementById(index + "").offsetHeight;
        arrH.push(h);
      });
      let newArr = [];
      let total = 0;
      arrH.forEach((heigth, index) => {
        let obj = {
          start: total,
          end: total + heigth,
          id: index,
        };
        newArr.push(obj);
        total += heigth;
      });
      return newArr;
    },
  },
  methods: {
    ...mapMutations(["CHANGE_FOOD_COUNT"]),
    // async getGoodsList() {
    //   const res = await api_goods_list();
    //   // console.log(res);
    //   if (res.data.code === 0) {
    //     // 去修改state中的goodsList
    //     this.SET_GOODS_LIST(res.data.data);

    //     // this.goodsList = res.data.data;
    //   }
    // },
    // 点击左侧导航
    sideBarClick(index) {
      console.log(index);
      // 通过索引获取右侧的目标元素
      // 不能用querySelector去获取元素，因为querySelector不支持数字的形式获取元素
      let targetEl = document.getElementById(index + "");
      this.betterScroll.scrollToElement(targetEl, 500);
    },
    // 改变商品数量
    handleChangeCount(id, type) {
      this.CHANGE_FOOD_COUNT({ id, type });
    },
    handleFoodJump(food) {
      console.log(111, food);
      this.$router.push({
        name: "detail",
        params: {
          ...food,
        },
      });
    },
  },
};
</script>

<style lang="less" scoped>
.goods-wrap {
  flex: 1;
  background-color: aqua;
  display: flex;
  overflow: hidden;
  .goods-left {
    background-color: antiquewhite;
    width: 80px;
    overflow-y: scroll;
    &::-webkit-scrollbar {
      width: 0;
    }
  }
  .goods-rigth {
    background-color: chartreuse;
    flex: 1;
    overflow-y: scroll;
    &::-webkit-scrollbar {
      width: 0;
    }
    .goods-wrap-cont {
      .goods-cate-wrap {
        background-color: #e3e3e3;
        height: 30px;
        line-height: 30px;
        padding: 0 10px;
        position: relative;
        &::after {
          content: "";
          position: absolute;
          top: 0;
          left: 0;
          height: 30px;
          width: 3px;
          background-color: blue;
        }
      }
      .goods-list {
        display: flex;
        background-color: #fff;
        height: 140px;
        align-items: center;
        padding: 0 10px;
        .list-left-img {
          width: 100px;
          height: 100px;
          .van-image {
            width: 100%;
            height: 100%;
          }
          border-radius: 5px;
          overflow: hidden;
        }
        .list-rigth-cont {
          flex: 1;
          padding-left: 10px;
          .money-count {
            display: flex;
            justify-content: space-between;
            align-items: center;
          }
        }
      }
    }
    .single-box {
      padding-bottom: 80px;
    }
  }
}
</style>